home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 655 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  70 lines

  1. Newsgroups: comp.lang.c
  2. Path: mxsld2.pd.infn.it!LORETI
  3. From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
  4. Subject: Re: gets() question
  5. X-Nntp-Posting-Host: mxsld2.pd.infn.it
  6. Message-ID: <DKupzL.7Ap@news.cern.ch>
  7. Sender: news@news.cern.ch (USENET News System)
  8. Reply-To: loreti@mxsld2.pd.infn.it
  9. Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
  10. References: <4cosgf$rir@newsbf02.news.aol.com>
  11. Date: Mon, 8 Jan 1996 07:25:48 GMT
  12.  
  13. In article <4cosgf$rir@newsbf02.news.aol.com>, simpsondg@aol.com (SimpsonDG) writes:
  14. >Can any of you C gurus out there help me with this?  I have a problem with
  15. >gets() that I don't understand.  The program below will successfully input
  16. >the string s[0], but will simply ignore the gets() that inputs s[1] and
  17. >goes on to ask for i[1].  What's the deal?  A little experimenting seems
  18. >to indicate that the problem arises when I have a gets() following a
  19. >scanf() -- e.g., a program using only gets() or only scanf() works fine.
  20. >
  21. >David Simpson
  22. >
  23. >--------------------------------------------------------------------------
  24. >-------------------
  25. >
  26. >#include "stdio.h"
  27. >
  28. >void main(void)
  29. >{
  30. >   int i[5];
  31. >   char s[3][10];
  32. >
  33. >   printf ("Enter s[0]:  ");
  34. >   gets (s[0]);
  35. >
  36. >   printf ("Enter i[0]]:  ");
  37. >   scanf ("%d",&i[0]);
  38. >
  39. >   printf ("Enter s[1]:  ");
  40. >   gets (s[1]);                  /* this gets() doesn't wait for input */
  41. >
  42. >   printf ("Enter i[1]]:  ");
  43. >   scanf ("%d",&i[1]);
  44. >}
  45.  
  46. This is covered in the comp.lang.c FAQ list, that you are supposed to
  47. check before posting, and that is available from RTFM.mit.edu in
  48. pub/usenet/comp.lang.c/*.
  49.  
  50. 12.18:  I'm reading a number with scanf %d and then a string with
  51.         gets(), but the compiler seems to be skipping the call to
  52.         gets()!
  53.  
  54. A:      scanf %d won't consume a trailing newline.  If the input number
  55.         is immediately followed by a newline, that newline will
  56.         immediately satisfy the gets().
  57.  
  58.         As a general rule, you shouldn't try to interlace calls to
  59.         scanf() with calls to gets() (or any other input routines);
  60.         scanf's peculiar treatment of newlines almost always leads to
  61.         trouble.  Either use scanf() to read everything or nothing.
  62.  
  63.         See also questions 12.20 and 12.23.
  64.  
  65.         References: ANSI Sec. 4.9.6.2; ISO Sec. 7.9.6.2; H&S Sec. 15.8
  66.         pp. 357-64.
  67. --
  68. Maurizio Loreti                       http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
  69. Un. of Padova, Dept. of Physics - Padova, Italy          loreti@padova.infn.it
  70.